local skel = fk.CreateSkill {
  name = "rmt__jiao",
}

Fk:loadTranslationTable{
  ["rmt__jiao"] = "极傲",
  [":rmt__jiao"] = "当你失去牌后，若你本轮未获得过牌，你可以摸两张牌；当你获得牌后，若你本轮未失去过牌，你可以弃置两张牌。",

  ["#rmt__jiao-draw"] = "极傲：是否摸两张牌？",
  ["#rmt__jiao-discard"] = "极傲：是否弃置两张牌？",

  ["$rmt__jiao1"] = "宦海如薄冰，求生逐富贵。",
  ["$rmt__jiao2"] = "吾不欲为鱼肉，故为刀俎。",
}

skel:addEffect(fk.AfterCardsMove, {
  anim_type = "drawcard",
  audio_index = 1,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(skel.name) then return false end
    for _, move in ipairs(data) do
      if move.from == player and (move.extra_data or {}).rmt__jiao1 then
        return true
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(player, { skill_name = skel.name, prompt = "#rmt__jiao-draw"})
  end,
  on_use = function(self, event, target, player, data)
    player:drawCards(2, skel.name)
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  anim_type = "negative",
  audio_index = 2,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(skel.name) or #player:getCardIds("he") < 2 then return false end
    for _, move in ipairs(data) do
      if move.to == player and (move.extra_data or {}).rmt__jiao2 then
        return true
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local cards = player.room:askToDiscard(player,{
      min_num = 2, max_num = 2, skill_name = skel.name, cancelable = true, include_equip = true,
      skip = true, prompt = "#rmt__jiao-discard",
    })
    if #cards == 2 then
      event:setCostData(self, {cards = cards})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:throwCard(event:getCostData(self).cards, skel.name, player, player)
  end,
})

-- 轮次间隙不能查记录器，还是用标记吧……
skel:addEffect(fk.AfterCardsMove, {
  refresh_events = {fk.AfterCardsMove},
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(skel.name, true) and (player:getMark("jiao_lose-round") == 0 or player:getMark("jiao_get-round") == 0)
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    for _, move in ipairs(data) do
      if move.from == player then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
            if player:getMark("jiao_lose-round") == 0 then
              room:setPlayerMark(player, "jiao_lose-round", 1)
              move.extra_data = move.extra_data or {}
              move.extra_data.rmt__jiao1 = true
              break
            end
          end
        end
      end
      if move.to == player and move.toArea == Card.PlayerHand then
        if player:getMark("jiao_get-round") == 0 then
          room:setPlayerMark(player, "jiao_get-round", 1)
          move.extra_data = move.extra_data or {}
          move.extra_data.rmt__jiao2 = true
        end
      end
    end
  end,
})

return skel
